//
//  CQStatusPictureView.swift
//  weibo
//
//  Created by chengqiang on 16/7/24.
//  Copyright © 2016年 chengqiangBiaodashi. All rights reserved.
//

import UIKit

let StatusPictureViewItemWH=((UIScreen.width()-2*CellMargin-(CGFloat(maxOfColumn)-1)*CGFloat(itemMargin)))/CGFloat(maxOfColumn)

//最大列数
let maxOfColumn = 3
//图片之间的间距
let itemMargin :CGFloat = 4
class CQStatusPictureView: UIView {

    var imageUrl:[NSURL]?{
        didSet{
           let size = layoutImages(imageUrl)
            self.snp_updateConstraints { (make) -> Void in
                make.size.equalTo(size)
            }
        }
    }
    
    //布局九个imagView的位置
    
    private func layoutImages(imageUrls:[NSURL]?)->CGSize{
        
        //获取图片的总个数
        let count = imageUrls?.count ?? 0
        
//        debuglable.text="图片的数量：\(count)"
        //获取图片的列数
        var numberOfColumn = maxOfColumn
        
        if count == 1{
            numberOfColumn = 1
        }else if count == 2 || count == 4{
            numberOfColumn = 2
        }
        
        
        for(index,imageView) in imageViews.enumerate(){
            
            if index>=count{
                imageView.hidden = true
            }else{
                imageView.hidden = false
                let column = index % numberOfColumn
                let row = index / numberOfColumn
                let x = CGFloat(column) * (StatusPictureViewItemWH + itemMargin)
                let y = CGFloat(row) * (StatusPictureViewItemWH + itemMargin)
                
                imageView.frame = CGRect(x: x, y: y, width: StatusPictureViewItemWH, height: StatusPictureViewItemWH)
                
                let url = imageUrls![index]
                imageView.sd_setImageWithURL(url)
            }
            
        }
        
        if count == 0 {
            return CGSizeZero
        }
        
        //宽度＝总列数＊图片的宽度＋（总列数－1）＊图片间距
        let  width = CGFloat(numberOfColumn)*StatusPictureViewItemWH+(CGFloat(numberOfColumn)-1)*itemMargin
        //总行数＝（总数＋总列数－1）／列数
        let numberOfRow = (count + numberOfColumn-1)/numberOfColumn
        // 计算高度＝总行数＊图片高度＋（总行数－1）＊图片的间距
        let height = CGFloat(numberOfRow)*StatusPictureViewItemWH+(CGFloat(numberOfRow)-1)*itemMargin
        return CGSize(width: width, height: height)
    }
    //存放九张图片数组
    private var imageViews:[UIImageView]=[UIImageView]()
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor=UIColor.whiteColor()
        
        setupUI()
    }
    //创建九个imageView
    private func setupUI(){
        for _ in 0..<9 {
            let imageView = UIImageView()
            
            imageView.backgroundColor=UIColor.whiteColor()
            //设置图片填充模式
            imageView.contentMode = UIViewContentMode.ScaleAspectFit
            imageView.layer.masksToBounds = true
            //添加到父控件 和 数组
            self.addSubview(imageView)
            imageViews.append(imageView)
        }
//        self.addSubview(debuglable)
//        debuglable.frame=CGRect(x: 0, y: 0, width: 300, height: 200)
        
    }
//    private lazy var debuglable :UILabel = {
//       let lable = UILabel(textColor: UIColor.blackColor(), fontSize: 20)
//        lable.text="时间快点哈"
//        lable.numberOfLines=0
//        return lable
//    }()
}
